package raone;


import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

class MyThread implements Runnable {
	  String name; // name of thread

	  Thread t;
	  Socket ClientSocket;
	  int Index;
	  public static int estblish_write(String x) throws IOException
		{
		String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		int fail=0;
		FileWriter fw;
		fw = new FileWriter("Log.txt",true);
		String temp = x;
		temp = " | Connection Successful with :" + temp;
		fw.write(sdf.format(cal.getTime()) +temp  +  System.getProperty( "line.separator" ));
		fw.write('\n');
		fw.close();
		return fail;
			
		}
		public static int fail_write(String x) throws IOException
		{
		String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);		    
		int fail=0;
		FileWriter fw;
		fw = new FileWriter("Log.txt",true);
		String temp = x;
		temp = " | Connection Failed with :" + temp;
		fw.write(sdf.format(cal.getTime()) +temp +  System.getProperty( "line.separator" ));
		fw.close();
		return fail;
			
		}
	 public MyThread(Socket x,int index, String threadname)
		{
			ClientSocket = x;
		    Index = index;
			name = threadname;
			t = new Thread(this, name);
			//System.out.println("New thread: " + t);
		    t.start();
		}

	@SuppressWarnings("deprecation")
	public void run(){
		 int flag=0;
			BufferedReader in;
			PrintWriter out;
			try {
				in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
				out = new PrintWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));
			    String string;
				while((string=in.readLine())!=null)
				{
					if(string.compareToIgnoreCase("1")==0)
					{
					System.out.println("Client Number: "+ Index + "Alive"+ "value Recieved :" + string);
					flag=1;
					}
					
					else if(string.compareToIgnoreCase("0")==0)
						{
						System.out.println("Client Number: "+ Index + "Dead" + "value Recieved :" + string);
						flag=1;
						break;
						}
					else if(string.compareToIgnoreCase("killed")==0)
					{
					throw new SocketTimeoutException ();
					}
					//out.println(string+"-reply from server ");
					//out.flush();
					
				}
			}
			catch (SocketTimeoutException s)
			{
				try {
					System.out.println("Marr gya Client" + Index);
					fail_write(ClientSocket.getInetAddress().getHostAddress());
				} 
				catch (IOException e) 
				{
					
					e.printStackTrace();
				}
				t.stop();
			}
			catch (IOException e1) 
			{
				// TODO Auto-generated catch block
				flag =0;
				e1.printStackTrace();
			}
			finally
				{
							
					Server_T.Thread_result[Index]=flag;
					try {
					
						if(flag==0)
							{System.out.println("Finally mein fail");
							fail_write(ClientSocket.getInetAddress().getHostAddress());}
						else
							estblish_write(ClientSocket.getInetAddress().getHostAddress());
						} 
					catch (IOException e1) 
						{
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					System.out.println("Setting Flag Value");
					try {
						ClientSocket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		
	}

	}

/* class MainClass {
  public static void main(String args[]) {
    MyThread ob1 = new MyThread("One");
    MyThread ob2 = new MyThread("Two");
    MyThread ob3 = new MyThread("Three");

    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());

    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }

    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());

    System.out.println("Main thread exiting.");
  }
}
    */      